using System;
using ControllerTypeValue = System.Int32;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Life.UI.MVC.Model
{
    public abstract class ModelBase
    {
        /// <summary>
        /// 当前Model的Token
        /// </summary>
        [ControllerTypeSelect]
        public ControllerTypeValue Token;

        public ModelBase(ControllerTypeValue token)
        {
            Token = token;
        }
        
        [Obsolete("建议子类实现ModelBase(ControllerTypeValue token)")]
        public ModelBase()
        {
            
        }
        
        /// <summary>
        /// 是否立即激活接受数据的页面、为false时表示默认处理时不对页面进行激活（或隐藏）操作
        /// </summary>
        public bool ActivateNow;

        public bool ClearView = true;
    }

    public abstract class PageArchiveDataBase : ICloneable
    {
        public ControllerTypeValue Token { get; protected set; }
        
        public abstract object Clone();
    }

    public sealed class PageArchiveModel : ModelBase
    {
        public PageArchiveModel(PageArchiveDataBase archiveData) : base(archiveData.Token)
        {
            ArchiveData = archiveData;
            ActivateNow = true;
        }
        public PageArchiveDataBase ArchiveData { get;private set; }
    }

    /// <summary>
    /// 内置的状态Model 用于控制UI的显示和隐藏
    /// </summary>
    public sealed class StateModel : ModelBase
    {
        public StateModel(int token):base(token)
        {
            
        }
        /// <summary>
        /// UI的显示和隐藏状态
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 往Controller子类传递,父类不拦截与处理的标记 为true时将传递给子类，且由子类负责全部操作
        /// </summary>
        public bool TransferTag { get; set; }
    }

    public static class ModelExtensions
    {
        public static List<T> DeepCopy<T>(this List<T> source) where T : class, ICloneable
        {
            var result = new List<T>(source.Count);
            foreach (var item in source)
            {
                result.Add((T)item.Clone());
            }
            return result;
        }
        
        public static T DeepCopy<T>(this T source) where T : class, ICloneable
        {
            return (T)source.Clone();
        }

        public static List<T> DeepCopy<T>(this List<T> source, Func<T, T> cloneFunc)
        {
            var result = new List<T>(source.Count);
            foreach (var item in source)
            {
                result.Add(cloneFunc(item));
            }
            return result;
        }
    }
}



